﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using TTQT.TTQT_Code;

/// <summary>
/// Summary description for SanPham
/// </summary>
public class YKienKhachHangDAO
{
    public string ID = "";
    public string date = "";
    public string Email = "";
    public string FullName = "";
    public string address = "";
    public string Phone = "";
    public string IP = "";
    public string parentId = "-1";
    public string detail = "";
    public string status = "";
    public string orderNum = "";

    public int insert()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("YKIENKHACHHANG_Insert", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@email", Email);
        cmd.Parameters.AddWithValue("@FullName", FullName);
        cmd.Parameters.AddWithValue("@address", address);
        cmd.Parameters.AddWithValue("@Phone", Phone);
        cmd.Parameters.AddWithValue("@IP", IP);
        cmd.Parameters.AddWithValue("@parentId", parentId);
        cmd.Parameters.AddWithValue("@detail", detail);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteScalar(), 0);
            ID = kq.ToString();
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public void yKienKhachHang_ToggleColumnValue(string colName, string id, string value)
    {
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        string sql = "", giaTri = "";
        if (value.Equals("1"))
        {
            giaTri = "2";
        }
        else
        {
            giaTri = "1";
        }
        sql = "Update YKienKhachHang set " + colName + "= " + giaTri + " where id=" + id;
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.CommandType = CommandType.Text;
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
    }
    public int delete()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("YKIENKHACHHANG_Delete", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ID", ID);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }


    public PagingOpbject getForDisplay_Paging(int _status, int pageSize, int currentPage)
    {
        PagingOpbject kq = new PagingOpbject();
        DataTable result = null;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("YKIENKHACHHANG_GetForDisplay_Paging", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.Add("@status", SqlDbType.Int, 4).Value = _status;

        comm.Parameters.Add("@PageSize", SqlDbType.Int, 4).Value = pageSize;
        comm.Parameters.Add("@CurrentPage", SqlDbType.Int, 4).Value = currentPage;
        comm.Parameters.Add("@ItemCount", SqlDbType.Int).Direction = ParameterDirection.Output;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
            kq.tableReturn = result;
            kq.totalRecords = Convert.ToInt32(comm.Parameters["@ItemCount"].Value);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    #region Cac phuong thuc cho quan tri
    public DataTable getByStatus(string status)
    {
        DataTable result = null;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("YKIENKHACHHANG_getByStatus", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@status", status);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    //public DataTable tinTuc_Nhom_GetAllForAdmin()
    //{
    //    DataTable result = null;

    //    SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
    //    SqlCommand comm = new SqlCommand("TinTuc_Nhom_GetAllForAdmin", conn);
        
    //    comm.CommandType = CommandType.StoredProcedure;
    //    comm.Parameters.Clear();
    //    try
    //    {
    //        conn.Open();
    //        result = new DataTable();
    //        SqlDataAdapter da = new SqlDataAdapter(comm);
    //        da.Fill(result);
    //    }
    //    catch (Exception exx)
    //    {
    //        // Writing log methods here.
    //    }
    //    finally
    //    {
    //        if (conn.State == ConnectionState.Open) conn.Close();
    //        conn.Dispose();
    //        comm.Dispose();
    //    }
    //    return result;
    //}
    //public string tinTuc_GetByNhomId_Json(string nhomId, string active)
    //{
    //    string kq = "";
    //    DataTable tb = tinTuc_GetByNhomAndIsActive(nhomId, active);
    //    ArrayList infos = new ArrayList();
    //    foreach (DataRow r in tb.Rows)
    //    {
    //        TinTucInfo info = new TinTucInfo();
    //        info.NewsId = r["NewsId"].ToString();
    //        info.CategoryId = r["CategoryId"].ToString();
    //        info.Title = r["Title"].ToString();
    //        info.ImagePath = r["ImagePath"].ToString();
    //        info.Summary = r["Summary"].ToString();
    //        info.Createdate = r["Createdate"].ToString();
    //        infos.Add(info);
    //    }
    //    kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
    //    return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    //}
    ///// <summary>
    ///// Dung trong quan tri. Lay danh sach tin theo nhom, active
    ///// </summary>
    //public DataTable tinTuc_GetByNhomAndIsActive(string categoryId,string isActive)
    //{
    //    DataTable result = null;

    //    SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
    //    SqlCommand comm = new SqlCommand("TinTuc_GetByNhomAndIsActive", conn);
    //    comm.CommandType = CommandType.StoredProcedure;
    //    comm.Parameters.Clear();
    //    comm.Parameters.Add("@CategoryId", SqlDbType.Int, 4).Value = categoryId;
    //    comm.Parameters.Add("@isActive", SqlDbType.Int, 4).Value = isActive;
    //    try
    //    {
    //        conn.Open();
    //        result = new DataTable();
    //        SqlDataAdapter da = new SqlDataAdapter(comm);
    //        da.Fill(result);
    //    }
    //    catch (Exception exx)
    //    {
    //        // Writing log methods here.
    //    }
    //    finally
    //    {
    //        if (conn.State == ConnectionState.Open) conn.Close();
    //        conn.Dispose();
    //        comm.Dispose();
    //    }
    //    return result;
    //}
    //public string news_GetTopByCategory_Json(string num, string categoryId)
    //{
    //    string kq = "";
    //    DataTable tb = News_GetTopByCategory(num, categoryId);
    //    ArrayList infos = new ArrayList();
    //    foreach (DataRow r in tb.Rows)
    //    {
    //        TinTucInfo info = new TinTucInfo();
    //        info.NewsId = r["NewsId"].ToString();
    //        info.CategoryId = r["CategoryId"].ToString();
    //        info.Title = r["Title"].ToString();
    //        info.ImagePath = r["ImagePath"].ToString();
    //        info.Summary = r["Summary"].ToString();
    //        info.Createdate = r["Createdate"].ToString();
    //        infos.Add(info);
    //    }
    //    kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
    //    return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    //}
    ///// <summary>
    ///// Dung khi hien thi tin tuc cho khach hang
    ///// </summary>
    ///// <param name="num">So luong ban ghi can lay</param>
    ///// <returns>DataTable de bind len man hinh xem</returns>
    //public DataTable News_GetTopByCategory(string num, string categoryId)
    //{
    //    DataTable result = null;

    //    SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
    //    SqlCommand comm = new SqlCommand("News_GetTopByCategory", conn);
    //    comm.CommandType = CommandType.StoredProcedure;
    //    comm.Parameters.Clear();
    //    comm.Parameters.Add("@num", SqlDbType.Int, 4).Value = num;
    //    comm.Parameters.Add("@categoryId", SqlDbType.Int, 4).Value = categoryId;
    //    try
    //    {
    //        conn.Open();
    //        result = new DataTable();
    //        SqlDataAdapter da = new SqlDataAdapter(comm);
    //        da.Fill(result);
    //    }
    //    catch (Exception exx)
    //    {
    //        // Writing log methods here.
    //    }
    //    finally
    //    {
    //        if (conn.State == ConnectionState.Open) conn.Close();
    //        conn.Dispose();
    //        comm.Dispose();
    //    }
    //    return result;
    //}

    ///// <summary>
    ///// Lay cac tin khac phia duoi muc tin
    ///// </summary>
    ///// <param name="num"></param>
    ///// <param name="numToSkip"></param>
    ///// <param name="categoryId"></param>
    ///// <returns></returns>
    //public DataTable News_GetOtherByCategory(string num,string numToSkip, string categoryId)
    //{
    //    DataTable result = null;

    //    SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
    //    SqlCommand comm = new SqlCommand("News_GetOtherByCategory", conn);
    //    comm.CommandType = CommandType.StoredProcedure;
    //    comm.Parameters.Clear();
    //    comm.Parameters.Add("@num", SqlDbType.Int, 4).Value = num;
    //    comm.Parameters.Add("@numtoSkip", SqlDbType.Int, 4).Value = numToSkip;
    //    comm.Parameters.Add("@categoryId", SqlDbType.Int, 4).Value = categoryId;
        
    //    try
    //    {
    //        conn.Open();
    //        result = new DataTable();
    //        SqlDataAdapter da = new SqlDataAdapter(comm);
    //        da.Fill(result);
    //    }
    //    catch (Exception exx)
    //    {
    //        // Writing log methods here.
    //    }
    //    finally
    //    {
    //        if (conn.State == ConnectionState.Open) conn.Close();
    //        conn.Dispose();
    //        comm.Dispose();
    //    }
    //    return result;
    //}
    ///// <summary>
    ///// Lấy tốp num các tin có cùng mục sau tin hiện tại
    ///// </summary>
    ///// <param name="num"></param>
    ///// <param name="newsId"></param>
    ///// <returns></returns>
    //public string News_getOtherByNewsId_Json(string num, string newsId)
    //{
    //    string kq = "";
    //    DataTable tb = News_getOtherByNewsId(num, newsId);
    //    ArrayList infos = new ArrayList();
    //    foreach (DataRow r in tb.Rows)
    //    {
    //        TinTucInfo info = new TinTucInfo();
    //        info.NewsId = r["NewsId"].ToString();
    //        info.CategoryId = r["CategoryId"].ToString();
    //        info.Title = r["Title"].ToString();
    //        info.ImagePath = r["ImagePath"].ToString();
    //        info.Summary = r["Summary"].ToString();
    //        info.Createdate = r["Createdate"].ToString();
    //        infos.Add(info);
    //    }
    //    kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
    //    return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    //}
    //public DataTable News_getOtherByNewsId(string num, string newsId)
    //{
    //    DataTable result = null;

    //    SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
    //    SqlCommand comm = new SqlCommand("News_getOtherByNewsId", conn);
    //    comm.CommandType = CommandType.StoredProcedure;
    //    comm.Parameters.Clear();
    //    comm.Parameters.Add("@num", SqlDbType.Int, 4).Value = num;
    //    comm.Parameters.Add("@NewsId", SqlDbType.Int, 4).Value = newsId;
    //    try
    //    {
    //        conn.Open();
    //        result = new DataTable();
    //        SqlDataAdapter da = new SqlDataAdapter(comm);
    //        da.Fill(result);
    //    }
    //    catch (Exception exx)
    //    {
    //        // Writing log methods here.
    //    }
    //    finally
    //    {
    //        if (conn.State == ConnectionState.Open) conn.Close();
    //        conn.Dispose();
    //        comm.Dispose();
    //    }
    //    return result;
    //}
    #endregion
}
